//
// Created by Tony on 2024/7/9.
//

#include "SymRef.h"
#include "../ClassTable.h"
#include "ConstantPool.h"
#include <memory>

namespace RtCode {

uint16_t SymRef::getClassNameIndex() const { return classNameIndex; }

std::string SymRef::className() const {
    auto pool = getConstantPool();
    const auto* cStr = pool->getUtf8(classNameIndex);
    return string{cStr};
}

std::shared_ptr<ConstantPool> SymRef::getConstantPool() const { return mConstantPool.lock(); }

std::shared_ptr<Class> SymRef::resolvedClass() {
    auto clazz = mClass.lock();
    if (clazz != nullptr) {
        return clazz;
    }

    shared_ptr<ClassTable> classTable = getConstantPool()->getOwnerClass()->getClassTable();
    clazz = classTable->getClassByName(className());
    mClass = clazz;
    return clazz;
}

} // namespace RtCode
